using MicroMsg.Common.Event;
using MicroMsg.Common.Utils;
using MicroMsg.Resource.@string;
using MicroMsg.Scene;
using MicroMsg.Scene.Video;
using MicroMsg.Scene.Voice;
using MicroMsg.Storage;
using MicroMsg.UI.UserContrl;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;
namespace MicroMsg.Manager
{
	public class ChatMsgMgr
	{
		public class TalkContect
		{
			public MMObservableCollection<ChatMsg, ChatListItem> talkList;
			public string talker;
			public int nLeftMsgTotal;
			public List<ChatMsg> msgList;
			public ChatPageCtrl chatPage;
		}
		public class TalkerMsgInfo
		{
			public string talker;
			public List<ChatMsg> msgList;
		}
		public delegate void ExpandMsgListDelegate(ChatMsgMgr.TalkContect talkInfo);
		public const int RECEIVER = 0;
		public const int SENDER = 1;
		private const string TAG = "ChatMsgMgr";
		private const int msgPerReadAmount = 10;
		private const int msgReadAmount = 10;
		private const int MaxPerPageCount = 60;
		public static List<Action> cleanCBList = new List<Action>();
		private static bool bRegisterEventNotify = false;
		private static Dictionary<string, ChatMsgMgr.TalkContect> mapAttachTalkList = new Dictionary<string, ChatMsgMgr.TalkContect>();
		public static void registerCleanMsg(Action cleaner)
		{
			if (ChatMsgMgr.cleanCBList.Contains(cleaner))
			{
				return;
			}
			ChatMsgMgr.cleanCBList.Add(cleaner);
		}
		public static bool updateListByMsgSvrID(List<ChatMsg> msgList)
		{
			return StorageMgr.chatMsg.updateListByMsgSvrID(msgList);
		}
		public static bool GetChatMsgInfo(ChatMsg chatMsg_0, ref string sender, ref string content)
		{
			if (chatMsg_0 == null)
			{
				return false;
			}
			if (string.IsNullOrEmpty(chatMsg_0.strMsg))
			{
				return false;
			}
			int num = chatMsg_0.strMsg.IndexOf(":\n");
			if (num >= 0 && num <= Enumerable.Count<char>(chatMsg_0.strMsg) - 2)
			{
				content = chatMsg_0.strMsg.Substring(num + 2);
				sender = chatMsg_0.strMsg.Substring(0, num);
				return true;
			}
			return false;
		}
		public static bool getMsgUser(ChatMsg chatMsg_0, ref string user, ref string content)
		{
			if (chatMsg_0 == null)
			{
				return false;
			}
			if (string.IsNullOrEmpty(chatMsg_0.strTalker))
			{
				return false;
			}
			if (chatMsg_0.strMsg == null)
			{
				chatMsg_0.strMsg = "";
			}
			user = chatMsg_0.strTalker;
			content = chatMsg_0.strMsg;
			if (chatMsg_0.nIsSender == 1)
			{
				if (ContactMgr.getUserType(chatMsg_0.strTalker) == ContactMgr.UserType.UserTypeQQ)
				{
					user = AccountMgr.getCurAccount().nBindQQ.ToString() + "@qqim";
					content = chatMsg_0.strMsg;
				}
				else
				{
					if (ContactMgr.getUserType(chatMsg_0.strTalker) == ContactMgr.UserType.UserTypeBottle)
					{
						user = AccountMgr.strUsrName + "@bottle";
						content = chatMsg_0.strMsg;
					}
					else
					{
						user = AccountMgr.getCurAccount().strUsrName;
						content = chatMsg_0.strMsg;
					}
				}
				return true;
			}
			if (ContactMgr.getUserType(chatMsg_0.strTalker) == ContactMgr.UserType.UserTypeChatRoom)
			{
				if (chatMsg_0.IsVideo())
				{
					string strMsg = chatMsg_0.strMsg;
					MsgTrans msgTrans = new MsgTrans();
					if (DownloadVideoService.parseVideoMsgXML(strMsg, msgTrans))
					{
						user = msgTrans.strFromUserName;
						content = chatMsg_0.strMsg;
					}
					else
					{
						Log.smethod_1("ChatMsgMgr", "parseVideoMsgXML false:" + strMsg);
					}
				}
				else
				{
					if (!string.IsNullOrEmpty(chatMsg_0.strMsg))
					{
						if (ChatMsgMgr.GetChatMsgInfo(chatMsg_0, ref user, ref content))
						{
							return true;
						}
						user = chatMsg_0.strTalker;
						content = chatMsg_0.strMsg;
					}
				}
			}
			else
			{
				user = chatMsg_0.strTalker;
				content = chatMsg_0.strMsg;
			}
			return true;
		}
		public static string GetNotifyContent(ChatMsg chatMsg_0)
		{
			if (chatMsg_0 == null)
			{
				return null;
			}
			if (chatMsg_0.nIsSender == 1)
			{
				return null;
			}
			string username = "";
			string text = "";
			if (!ChatMsgMgr.getMsgUser(chatMsg_0, ref username, ref text))
			{
				Log.smethod_1("ChatMsgMgr", "GetNotifyContent error:content:" + chatMsg_0.strMsg);
				return null;
			}
			if (chatMsg_0.IsTxt())
			{
				return ContactHelper.getDisplayName(username, null) + ": " + text;
			}
			if (chatMsg_0.IsImage())
			{
				return string.Format(strings.ChatNewImgHint, ContactHelper.getDisplayName(username, null));
			}
			if (chatMsg_0.IsVoice())
			{
				return string.Format(strings.ChatNewVoiceHint, ContactHelper.getDisplayName(username, null));
			}
			if (chatMsg_0.IsShareCard())
			{
				return string.Format(strings.ChatNewCardHint, ContactHelper.getDisplayName(username, null));
			}
			if (chatMsg_0.IsVideo())
			{
				return string.Format(strings.ChatNewVideoHint, ContactHelper.getDisplayName(username, null));
			}
			if (chatMsg_0.IsCustomSmiley())
			{
				return string.Format(strings.ChatNewCustomSmileyHint, ContactHelper.getDisplayName(username, null));
			}
			return null;
		}
		public static string GetNotifyContentForNewMsgTips(ChatMsg chatMsg_0)
		{
			if (chatMsg_0 == null)
			{
				return null;
			}
			if (chatMsg_0.nIsSender == 1)
			{
				return null;
			}
			string username = "";
			string text = "";
			if (!ChatMsgMgr.getMsgUser(chatMsg_0, ref username, ref text))
			{
				Log.smethod_1("ChatMsgMgr", "GetNotifyContent error:content:" + chatMsg_0.strMsg);
				return null;
			}
			string text2 = ContactHelper.getDisplayName(username, null) + ": ";
			if (chatMsg_0.IsAppMsg())
			{
				return text2 + ChatMsgMgr.getAppMsgContent(chatMsg_0);
			}
			if (chatMsg_0.IsTxt())
			{
				return text2 + text;
			}
			if (chatMsg_0.IsImage())
			{
				return text2 + strings.ChatImgContent;
			}
			if (chatMsg_0.IsVoice())
			{
				return text2 + strings.ChatVoiceContent;
			}
			if (chatMsg_0.IsShareCard())
			{
				return text2 + "[" + strings.Chat_ShareCard + "]";
			}
			if (chatMsg_0.IsVideo())
			{
				return text2 + strings.ChatVideoContent;
			}
			if (chatMsg_0.IsMap())
			{
				return text2 + strings.Chat_My_Location;
			}
			if (chatMsg_0.IsCustomSmiley())
			{
				return text2 + strings.ChatCustomSmileyContent;
			}
			return null;
		}
		public static MsgReadStatus getVoiceStatus(ChatMsg chatMsg_0)
		{
			if (chatMsg_0 == null)
			{
				return MsgReadStatus.MsgReaded;
			}
			if (!chatMsg_0.IsVoice())
			{
				return MsgReadStatus.MsgReaded;
			}
			if (chatMsg_0.nIsSender == 1)
			{
				return MsgReadStatus.MsgReaded;
			}
			if (ChatMsgMgr.getVoiceMsgIsRead(chatMsg_0))
			{
				return MsgReadStatus.MsgReaded;
			}
			return MsgReadStatus.MsgUnRead;
		}
		public static bool getVoiceMsgIsRead(ChatMsg chatMsg_0)
		{
			return chatMsg_0 != null && chatMsg_0.IsVoice() && chatMsg_0.isVoiceRead;
		}
		public static void setVoiceMsgIsRead(ChatMsg chatMsg_0, bool isRead)
		{
			if (chatMsg_0 != null && chatMsg_0.IsVoice())
			{
				chatMsg_0.isVoiceRead = isRead;
				StorageMgr.chatMsg.modifyMsg(chatMsg_0);
				return;
			}
		}
		public static string getMsgThumbnail(ChatMsg chatMsg_0)
		{
			if (chatMsg_0 == null)
			{
				return null;
			}
			if (!chatMsg_0.IsImage() && !chatMsg_0.IsVideo() && !chatMsg_0.IsCustomSmiley() && !chatMsg_0.IsAppMsg())
			{
				return null;
			}
			return chatMsg_0.strThumbnail;
		}
		public static string getMsgImage(ChatMsg chatMsg_0)
		{
			if (!chatMsg_0.IsImage() && !chatMsg_0.IsCustomSmiley() && !chatMsg_0.IsAppMsg())
			{
				return null;
			}
			return chatMsg_0.strPath;
		}
		public static int downloadMsgImage(ChatMsg chatMsg_0, int compressType = 0)
		{
			if (chatMsg_0 != null && chatMsg_0.IsImage())
			{
				return ServiceCenter.sceneDownloadImg.doScene(chatMsg_0.nMsgSvrID, chatMsg_0.strTalker, compressType);
			}
			return 0;
		}
		public static bool hasFullImgToDownload(ChatMsg chatMsg_0)
		{
			if (chatMsg_0 == null || !chatMsg_0.IsImage())
			{
				return false;
			}
			int num = 0;
			int num2 = 0;
			if (!ChatMsgMgr.getImageLenFromContent(chatMsg_0, ref num, ref num2))
			{
				Log.smethod_1("ChatMsgMgr", "xml parse error");
				return false;
			}
			if (num2 != 0)
			{
				if (num2 != num)
				{
					return true;
				}
			}
			return false;
		}
		public static string getMsgVoice(ChatMsg chatMsg_0)
		{
			if (chatMsg_0 == null || !chatMsg_0.IsVoice())
			{
				return null;
			}
			if (chatMsg_0.strPath != null)
			{
				return chatMsg_0.strPath;
			}
			if (chatMsg_0.nIsSender == 1)
			{
				return MsgVoiceStorage.getUploadVoiceFilePath(chatMsg_0.strClientMsgId);
			}
			return MsgVoiceStorage.getDownloadVoiceFilePath(chatMsg_0.nMsgSvrID);
		}
		public static void deleteMsgByTalker(string talker)
		{
			if (string.IsNullOrEmpty(talker))
			{
				return;
			}
			int msgNum = StorageMgr.chatMsg.getMsgNum(talker);
			if (msgNum <= 0)
			{
				return;
			}
			List<ChatMsg> msgList = StorageMgr.chatMsg.getMsgList(talker, 0, msgNum);
			if (msgList != null)
			{
				using (List<ChatMsg>.Enumerator enumerator = msgList.GetEnumerator())
				{
					while (enumerator.MoveNext())
					{
						ChatMsg current = enumerator.get_Current();
						ChatMsgMgr.deleteMsgData(current);
					}
				}
			}
			StorageMgr.chatMsg.delByTalker(talker);
		}
		public static void deleteMsgData(ChatMsg chatMsg_0)
		{
			if (chatMsg_0 == null)
			{
				return;
			}
			if (chatMsg_0.IsImage())
			{
				StorageIO.deleteFile(chatMsg_0.strThumbnail);
				StorageIO.deleteFile(chatMsg_0.strPath);
				return;
			}
			if (chatMsg_0.IsVoice())
			{
				StorageIO.deleteFile(ChatMsgMgr.getMsgVoice(chatMsg_0));
				return;
			}
			if (chatMsg_0.IsVideo())
			{
				StorageIO.deleteFile(chatMsg_0.strThumbnail);
				StorageIO.deleteFile(chatMsg_0.strPath);
			}
		}
		public static void ClearAllMsg()
		{
			ConversationMgr.clearAll();
			StorageMgr.chatMsg.clearAllMsg();
			StorageIO.deleteMsgData();
			using (List<Action>.Enumerator enumerator = ChatMsgMgr.cleanCBList.GetEnumerator())
			{
				while (enumerator.MoveNext())
				{
					Action current = enumerator.get_Current();
					current.Invoke();
				}
			}
		}
		public static void saveMsgThumbnail(int nMsgSvrID, string fromUser, string toUser, byte[] imgBuf)
		{
			MsgTrans msgTrans = StorageMgr.msgImg.updateThumbnailByMsgSvrID(nMsgSvrID, fromUser, toUser, imgBuf);
			if (msgTrans == null || string.IsNullOrEmpty(msgTrans.strThumbnail))
			{
				return;
			}
			ChatMsg bySvrID = StorageMgr.chatMsg.getBySvrID(ChatMsgHelper.getTalker(toUser, fromUser), nMsgSvrID);
			if (bySvrID == null)
			{
				return;
			}
			bySvrID.strThumbnail = msgTrans.strThumbnail;
			StorageMgr.chatMsg.modifyMsg(bySvrID);
			Log.smethod_5("ChatMsgMgr", "saveMsgThumbnail" + msgTrans.strThumbnail);
		}
		public static void saveVideoMsgThumbnail(int nMsgSvrID, string fromUser, string toUser, byte[] imgBuf)
		{
			MsgTrans msgTrans = StorageMgr.msgVideo.updateThumbnailByMsgSvrID(nMsgSvrID, fromUser, toUser, imgBuf);
			if (msgTrans == null || string.IsNullOrEmpty(msgTrans.strThumbnail))
			{
				return;
			}
			ChatMsg bySvrID = StorageMgr.chatMsg.getBySvrID(ChatMsgHelper.getTalker(toUser, fromUser), nMsgSvrID);
			if (bySvrID == null)
			{
				return;
			}
			bySvrID.strThumbnail = msgTrans.strThumbnail;
			bySvrID.nStatus = 3;
			StorageMgr.chatMsg.modifyMsg(bySvrID);
			Log.smethod_5("ChatMsgMgr", "saved video msg thumbnail = " + msgTrans.strThumbnail);
		}
		public static void attachTalker(MMObservableCollection<ChatMsg, ChatListItem> talkList, string talker, ChatPageCtrl chatPage)
		{
			if (string.IsNullOrEmpty(talker) || talkList == null || chatPage == null)
			{
				return;
			}
			if (!ChatMsgMgr.bRegisterEventNotify)
			{
				ChatMsgMgr.bRegisterEventNotify = true;
				EventCenter.registerEventHandler(EventConst.ON_STORAGE_CHAT_MSG_ADD, new EventHandlerDelegate(ChatMsgMgr.onTalkingMsgAdd));
				EventCenter.registerEventHandler(EventConst.ON_STORAGE_CHAT_MSG_MODIFY, new EventHandlerDelegate(ChatMsgMgr.onTalkingMsgModify));
				EventCenter.registerEventHandler(EventConst.ON_STORAGE_CHAT_MSG_DEL, new EventHandlerDelegate(ChatMsgMgr.onTalkingMsgDelete));
			}
			talkList.Clear();
			ChatMsgMgr.TalkContect talkContect;
			if (ChatMsgMgr.mapAttachTalkList.ContainsKey(talker))
			{
				talkContect = ChatMsgMgr.mapAttachTalkList.get_Item(talker);
			}
			else
			{
				talkContect = new ChatMsgMgr.TalkContect();
				ChatMsgMgr.mapAttachTalkList.set_Item(talker, talkContect);
			}
			talkContect.chatPage = chatPage;
			talkContect.talkList = talkList;
			talkContect.talker = talker;
			if (talker == "fmessage")
			{
				talkContect.nLeftMsgTotal = FMsgMgr.getMsgNum(FMsgType.other);
				int num = (talkContect.nLeftMsgTotal < 10) ? talkContect.nLeftMsgTotal : 10;
				int num2 = talkContect.nLeftMsgTotal - num;
				if (num <= 0)
				{
					return;
				}
				List<ChatMsg> msgList = FMsgMgr.getMsgList(FMsgType.other, num2, num, true);
				if (msgList != null && msgList.get_Count() > 0)
				{
					talkContect.nLeftMsgTotal -= msgList.get_Count();
					ChatTimeManager.AddMsgListToTop(talkList, msgList);
					return;
				}
				Log.smethod_1("ChatMsgMgr", "FMsgMgr read chat msg error" + num2 + num);
				return;
			}
			else
			{
				talkContect.nLeftMsgTotal = StorageMgr.chatMsg.getMsgNum(talker);
				int num3 = (talkContect.nLeftMsgTotal < 10) ? talkContect.nLeftMsgTotal : 10;
				int num4 = talkContect.nLeftMsgTotal - num3;
				if (num3 <= 0)
				{
					return;
				}
				List<ChatMsg> list = StorageMgr.chatMsg.getMsgList(talker, num4, num3);
				list = ChatMsgMgr.filterMsgByValueCnt(list);
				if (list != null && list.get_Count() > 0)
				{
					talkContect.nLeftMsgTotal -= list.get_Count();
					ChatTimeManager.AddMsgListToTop(talkList, list);
					return;
				}
				Log.smethod_1("ChatMsgMgr", "read chat msg error" + num4 + num3);
				return;
			}
		}
		private static List<ChatMsg> filterMsgByValueCnt(List<ChatMsg> listOrigin)
		{
			if (listOrigin == null)
			{
				return null;
			}
			List<ChatMsg> list = new List<ChatMsg>();
			int num = 0;
			for (int i = 1; i <= listOrigin.get_Count(); i++)
			{
				ChatMsg chatMsg = listOrigin.get_Item(listOrigin.get_Count() - i);
				if (chatMsg != null)
				{
					if (chatMsg.IsAppMsg())
					{
						num += 3;
					}
					else
					{
						num++;
					}
					if (num > 10)
					{
						break;
					}
					list.Insert(0, chatMsg);
				}
			}
			return list;
		}
		public static void detachTalker(string talker)
		{
			if (!string.IsNullOrEmpty(talker) && ChatMsgMgr.mapAttachTalkList.ContainsKey(talker))
			{
				ChatMsgMgr.TalkContect talkContect = ChatMsgMgr.mapAttachTalkList.get_Item(talker);
				talkContect.talkList.Clear();
				ChatMsgMgr.mapAttachTalkList.Remove(talker);
				if (ChatMsgMgr.mapAttachTalkList.get_Count() <= 0)
				{
					EventCenter.removeEventHandler(EventConst.ON_STORAGE_CHAT_MSG_ADD, new EventHandlerDelegate(ChatMsgMgr.onTalkingMsgAdd));
					EventCenter.removeEventHandler(EventConst.ON_STORAGE_CHAT_MSG_MODIFY, new EventHandlerDelegate(ChatMsgMgr.onTalkingMsgModify));
					EventCenter.removeEventHandler(EventConst.ON_STORAGE_CHAT_MSG_DEL, new EventHandlerDelegate(ChatMsgMgr.onTalkingMsgDelete));
					ChatMsgMgr.bRegisterEventNotify = false;
				}
				return;
			}
		}
		public static bool CanExpand(string talker)
		{
			if (!string.IsNullOrEmpty(talker) && ChatMsgMgr.mapAttachTalkList.ContainsKey(talker))
			{
				ChatMsgMgr.TalkContect talkContect = ChatMsgMgr.mapAttachTalkList.get_Item(talker);
				return talkContect.nLeftMsgTotal > 0;
			}
			return false;
		}
		public static int GetExpandNum(string talker)
		{
			if (string.IsNullOrEmpty(talker) || !ChatMsgMgr.mapAttachTalkList.ContainsKey(talker))
			{
				return 0;
			}
			ChatMsgMgr.TalkContect talkContect = ChatMsgMgr.mapAttachTalkList.get_Item(talker);
			int nLeftMsgTotal = talkContect.nLeftMsgTotal;
			if (nLeftMsgTotal <= 10)
			{
				return nLeftMsgTotal;
			}
			return 10;
		}
		public static bool ExpandTalkMsgList(string talker)
		{
			if (!ChatMsgMgr.CanExpand(talker))
			{
				return false;
			}
			ChatMsgMgr.TalkContect talkContect = ChatMsgMgr.mapAttachTalkList.get_Item(talker);
			int nLeftMsgTotal = talkContect.nLeftMsgTotal;
			int num = (nLeftMsgTotal > 10) ? 10 : nLeftMsgTotal;
			int num2 = talkContect.nLeftMsgTotal - num;
			List<ChatMsg> list;
			if (talkContect.talker == "fmessage")
			{
				list = FMsgMgr.getMsgList(FMsgType.other, num2, num, true);
			}
			else
			{
				list = StorageMgr.chatMsg.getMsgList(talkContect.talker, num2, num);
				list = ChatMsgMgr.filterMsgByValueCnt(list);
			}
			if (list != null && list.get_Count() > 0)
			{
				talkContect.nLeftMsgTotal -= list.get_Count();
				ChatTimeManager.AddMsgListToTop(talkContect.talkList, list);
				return true;
			}
			Log.smethod_1("ChatMsgMgr", "ExpandTalkMsgList read chat msg error" + num2 + num);
			return false;
		}
		private static List<ChatMsgMgr.TalkerMsgInfo> getCombineTalkerMsg(List<ChatMsg> listArgs)
		{
			if (listArgs != null && listArgs.get_Count() != 0)
			{
				List<ChatMsgMgr.TalkerMsgInfo> list = new List<ChatMsgMgr.TalkerMsgInfo>();
				Dictionary<string, int> dictionary = new Dictionary<string, int>();
				for (int i = 0; i < listArgs.get_Count(); i++)
				{
					ChatMsg chatMsg = listArgs.get_Item(i);
					if (chatMsg != null && !string.IsNullOrEmpty(chatMsg.strTalker) && (!(chatMsg.strTalker == "fmessage") || FMsgMgr.getFMsgType(chatMsg) == FMsgType.other))
					{
						if (dictionary.ContainsKey(chatMsg.strTalker))
						{
							int num = dictionary.get_Item(chatMsg.strTalker);
							list.get_Item(num).msgList.Add(chatMsg);
						}
						else
						{
							dictionary.set_Item(chatMsg.strTalker, list.get_Count());
							ChatMsgMgr.TalkerMsgInfo talkerMsgInfo = new ChatMsgMgr.TalkerMsgInfo();
							talkerMsgInfo.talker = chatMsg.strTalker;
							talkerMsgInfo.msgList = new List<ChatMsg>();
							talkerMsgInfo.msgList.Add(chatMsg);
							list.Add(talkerMsgInfo);
						}
					}
				}
				return list;
			}
			return null;
		}
		private static void onTalkingMsgAdd(EventWatcher watcher, BaseEventArgs evtArgs)
		{
			if (ChatMsgMgr.mapAttachTalkList.get_Count() <= 0)
			{
				return;
			}
			List<ChatMsg> listObject = evtArgs.getListObject<ChatMsg>();
			if (listObject == null)
			{
				return;
			}
			Log.smethod_5("ChatMsgMgr", "onTalkingMsgAdd:" + listObject.get_Count());
			List<ChatMsgMgr.TalkerMsgInfo> combineTalkerMsg = ChatMsgMgr.getCombineTalkerMsg(listObject);
			if (combineTalkerMsg != null && combineTalkerMsg.get_Count() > 0)
			{
				Log.smethod_5("ChatMsgMgr", "getCombineTalkerMsg:" + combineTalkerMsg.get_Count());
				using (List<ChatMsgMgr.TalkerMsgInfo>.Enumerator enumerator = combineTalkerMsg.GetEnumerator())
				{
					while (enumerator.MoveNext())
					{
						ChatMsgMgr.TalkerMsgInfo current = enumerator.get_Current();
						if (current != null && !string.IsNullOrEmpty(current.talker) && current.msgList != null && current.msgList.get_Count() > 0 && ChatMsgMgr.mapAttachTalkList.ContainsKey(current.talker))
						{
							ChatMsgMgr.TalkContect talkContect = ChatMsgMgr.mapAttachTalkList.get_Item(current.talker);
							if (talkContect != null && talkContect.talkList != null)
							{
								List<ChatMsg> msgList = current.msgList;
								int realMsgCount = ChatTimeManager.GetRealMsgCount(talkContect.talkList);
								if (msgList.get_Count() + realMsgCount > 60)
								{
									int num = 0;
									int num2;
									if (msgList.get_Count() >= 60)
									{
										num = msgList.get_Count() - 60;
										num2 = realMsgCount;
									}
									else
									{
										int num3 = 60 - msgList.get_Count();
										num2 = realMsgCount - num3;
									}
									talkContect.nLeftMsgTotal += num2;
									talkContect.nLeftMsgTotal += num;
									Log.smethod_5("ChatMsgMgr", "talkListToRemove:" + num2);
									Log.smethod_5("ChatMsgMgr", "msgListToRemove:" + num);
									for (int i = 0; i < num; i++)
									{
										msgList.RemoveAt(0);
									}
									if (num2 >= realMsgCount)
									{
										talkContect.talkList.Clear();
									}
									else
									{
										int j = 0;
										while (j < num2)
										{
											ChatMsg chatMsg = talkContect.talkList[0];
											if (!(chatMsg is ChatTimeMsg))
											{
												j++;
											}
											talkContect.talkList.RemoveAt(0);
										}
										ChatTimeManager.FillTimeMsg(talkContect.talkList);
									}
									ChatMsgMgr.AddMsgListToChatPage(talkContect, msgList);
									if (talkContect.chatPage != null)
									{
										talkContect.chatPage.RefreshMoreBtn();
									}
								}
								else
								{
									ChatMsgMgr.AddMsgListToChatPage(talkContect, msgList);
								}
							}
						}
					}
				}
				return;
			}
		}
		private static List<ChatMsg> pickUpReceiveMsg(List<ChatMsg> listArgs)
		{
			List<ChatMsg> list = new List<ChatMsg>();
			using (List<ChatMsg>.Enumerator enumerator = listArgs.GetEnumerator())
			{
				while (enumerator.MoveNext())
				{
					ChatMsg current = enumerator.get_Current();
					if (!current.IsSender())
					{
						list.Add(current);
					}
				}
			}
			return list;
		}
		private static bool isSupportUserType(ChatMsg chatMsg_0)
		{
			return chatMsg_0 != null && ContactMgr.getUserType(chatMsg_0.strTalker) == ContactMgr.UserType.UserTypeChatRoom;
		}
		private static string getNewMsgTips(ChatMsg chatMsg_0)
		{
			if (chatMsg_0 == null)
			{
				return null;
			}
			if (chatMsg_0.strTalker != null && !chatMsg_0.strTalker.Contains(ConstantValue.TAG_BOTTLE))
			{
				return ChatMsgMgr.GetNotifyContentForNewMsgTips(chatMsg_0);
			}
			return null;
		}
		public static string getAppMsgContent(ChatMsg chatMsg_0)
		{
			if (chatMsg_0 == null)
			{
				return strings.COM_Msg;
			}
			AppMsgInfo appMsgInfo = AppMsgMgr.parseAppXml(chatMsg_0.strMsg);
			string text = null;
			if (appMsgInfo != null && appMsgInfo.showtype == 1)
			{
				text = appMsgInfo.title;
			}
			if (appMsgInfo != null && appMsgInfo.type == 1)
			{
				text = appMsgInfo.title;
			}
			else
			{
				if (appMsgInfo != null && appMsgInfo.type == 2)
				{
					text = strings.ChatImgContent;
				}
				else
				{
					if (appMsgInfo != null && appMsgInfo.type == 3)
					{
						text = strings.ChatMusicContent;
					}
					else
					{
						if (appMsgInfo != null && appMsgInfo.type == 4)
						{
							text = strings.ChatVideoContent;
						}
						else
						{
							if (appMsgInfo != null && appMsgInfo.type == 5)
							{
								text = strings.ChatURLContent;
							}
							else
							{
								if (appMsgInfo != null && appMsgInfo.type == 6)
								{
									text = strings.ChatFileContent;
								}
								else
								{
									if (appMsgInfo != null && appMsgInfo.type == 7)
									{
										text = strings.ChatAppOpenContent;
									}
									else
									{
										Log.smethod_1("ChatMsgMgr", "invalid appmsg, xml = " + chatMsg_0.strMsg);
									}
								}
							}
						}
					}
				}
			}
			if (text == null)
			{
				text = strings.COM_Msg;
			}
			return text;
		}
		private static void ShowNewMsgTipsInChatPageCtrl(ChatMsgMgr.TalkContect talkInfo, List<ChatMsg> listArgs)
		{
			if (talkInfo == null || talkInfo.talkList == null || listArgs == null)
			{
				return;
			}
			List<ChatMsg> list = ChatMsgMgr.pickUpReceiveMsg(listArgs);
			if (list == null || Enumerable.Count<ChatMsg>(list) == 0)
			{
				return;
			}
			ChatPageCtrl chatPage = talkInfo.chatPage;
			if (chatPage == null)
			{
				return;
			}
			if (!chatPage.isShowByChatContainerPage())
			{
				return;
			}
			if (!ChatMsgMgr.isSupportUserType(list.get_Item(0)))
			{
				return;
			}
			int num = Enumerable.Count<ChatMsg>(list);
			string newMsgTips = ChatMsgMgr.getNewMsgTips(list.get_Item(num - 1));
			chatPage.recordNewMsgContent(newMsgTips, num);
		}
		public static void AddMsgListToChatPage(ChatMsgMgr.TalkContect talkInfo, List<ChatMsg> listArgs)
		{
			if (talkInfo != null && talkInfo.talkList != null && listArgs != null)
			{
				MMObservableCollection<ChatMsg, ChatListItem> talkList = talkInfo.talkList;
				using (List<ChatMsg>.Enumerator enumerator = listArgs.GetEnumerator())
				{
					while (enumerator.MoveNext())
					{
						ChatMsg current = enumerator.get_Current();
						if (current != null && !string.IsNullOrEmpty(current.strTalker) && ChatMsgMgr.mapAttachTalkList.ContainsKey(current.strTalker))
						{
							if (current.IsVoice() && current.GetStatus() == MsgUIStatus.Success)
							{
								VoiceService.AddReceived(current);
							}
							ChatTimeManager.AddMsgToBottom(talkList, current);
						}
					}
				}
				ChatMsgMgr.ShowNewMsgTipsInChatPageCtrl(talkInfo, listArgs);
				return;
			}
		}
		private static void onTalkingMsgModify(EventWatcher watcher, BaseEventArgs evtArgs)
		{
			if (ChatMsgMgr.mapAttachTalkList.get_Count() <= 0)
			{
				return;
			}
			List<ChatMsg> listObject = evtArgs.getListObject<ChatMsg>();
			if (listObject == null)
			{
				return;
			}
			using (List<ChatMsg>.Enumerator enumerator = listObject.GetEnumerator())
			{
				ChatMsg chatMsg_0;
				while (enumerator.MoveNext())
				{
					chatMsg_0 = enumerator.get_Current();
					if (chatMsg_0 != null && !string.IsNullOrEmpty(chatMsg_0.strTalker) && ChatMsgMgr.mapAttachTalkList.ContainsKey(chatMsg_0.strTalker))
					{
						ChatMsgMgr.TalkContect talkContect = ChatMsgMgr.mapAttachTalkList.get_Item(chatMsg_0.strTalker);
						if (talkContect != null)
						{
							ChatMsg chatMsg = Enumerable.FirstOrDefault<ChatMsg>(Enumerable.Where<ChatMsg>(talkContect.talkList, (ChatMsg chatMsg_1) => chatMsg_1.nMsgLocalID == chatMsg_0.nMsgLocalID));
							if (chatMsg != null)
							{
								int num = talkContect.talkList.IndexOf(chatMsg);
								if (num >= 0 && num < talkContect.talkList.get_Count())
								{
									talkContect.talkList[num] = chatMsg_0;
									if (chatMsg_0.IsVoice() && chatMsg_0.nIsSender == 0 && chatMsg_0.GetStatus() == MsgUIStatus.Success && ChatMsgMgr.getVoiceStatus(chatMsg_0) == MsgReadStatus.MsgUnRead)
									{
										VoiceService.AddReceived(chatMsg_0);
									}
								}
							}
						}
					}
				}
			}
		}
		private static void onTalkingMsgDelete(EventWatcher watcher, BaseEventArgs evtArgs)
		{
			if (ChatMsgMgr.mapAttachTalkList.get_Count() <= 0)
			{
				return;
			}
			List<ChatMsg> listObject = evtArgs.getListObject<ChatMsg>();
			if (listObject == null)
			{
				return;
			}
			using (List<ChatMsg>.Enumerator enumerator = listObject.GetEnumerator())
			{
				ChatMsg chatMsg_0;
				while (enumerator.MoveNext())
				{
					chatMsg_0 = enumerator.get_Current();
					if (chatMsg_0 != null && !string.IsNullOrEmpty(chatMsg_0.strTalker) && ChatMsgMgr.mapAttachTalkList.ContainsKey(chatMsg_0.strTalker))
					{
						ChatMsgMgr.TalkContect talkContect = ChatMsgMgr.mapAttachTalkList.get_Item(chatMsg_0.strTalker);
						if (talkContect != null)
						{
							ChatMsg chatMsg = Enumerable.FirstOrDefault<ChatMsg>(Enumerable.Where<ChatMsg>(talkContect.talkList, (ChatMsg chatMsg_1) => chatMsg_1.nMsgLocalID == chatMsg_0.nMsgLocalID));
							if (chatMsg != null)
							{
								int num = talkContect.talkList.IndexOf(chatMsg);
								if (num >= 0 && num < talkContect.talkList.get_Count())
								{
									ChatTimeManager.RemoveMsgAt(talkContect.talkList, num);
								}
							}
						}
					}
				}
			}
		}
		public static void deleteTalkMsg(ChatMsg chatMsg_0)
		{
			if (chatMsg_0 == null)
			{
				return;
			}
			StorageMgr.chatMsg.delMsg(chatMsg_0);
			ChatMsgMgr.deleteMsgData(chatMsg_0);
		}
		public static void resendTalkMsg(ChatMsg chatMsg_0, int mTransID = -1)
		{
			if (chatMsg_0 == null)
			{
				return;
			}
			if (chatMsg_0.nIsSender != 1)
			{
				return;
			}
			if (chatMsg_0.IsTxt() || chatMsg_0.isQmessageTxt())
			{
				Log.smethod_5("ChatMsgMgr", "resendTalkMsg txt:strClientMsgId " + chatMsg_0.strClientMsgId);
				ServiceCenter.sceneSendMsg.doSceneResendMsg(chatMsg_0);
				return;
			}
			if (chatMsg_0.IsVoice())
			{
				Log.smethod_5("ChatMsgMgr", "resendTalkMsg voice:strClientMsgId " + chatMsg_0.strClientMsgId);
				ServiceCenter.sceneUploadVoice.doSceneResume(chatMsg_0.strClientMsgId);
				return;
			}
			if (chatMsg_0.IsImage())
			{
				Log.smethod_5("ChatMsgMgr", "resendTalkMsg img:nMsgTransID " + chatMsg_0.nMsgLocalID);
				int num = mTransID;
				if (num == -1)
				{
					num = MsgTransStorageProxy.getMsgTransID(chatMsg_0.nMsgLocalID);
				}
				Log.smethod_5("ChatMsgMgr", string.Concat(new object[]
				{
					"Talker:",
					chatMsg_0.strTalker,
					"  transID:",
					num
				}));
				ServiceCenter.sceneSendImg.doScene(chatMsg_0.strTalker, num);
				return;
			}
			if (chatMsg_0.IsCustomSmiley())
			{
				Log.smethod_5("ChatMsgMgr", "resendTalkMsg CustomSmiley:nMsgTransID " + chatMsg_0.nMsgLocalID);
				new NetSceneEmojiUploadImage().reSendEmoji(chatMsg_0);
				return;
			}
			if (chatMsg_0.IsVideo())
			{
				Log.smethod_5("ChatMsgMgr", "resendTalkMsg CustomSmiley:nMsgTransID " + chatMsg_0.nMsgLocalID);
				ServiceCenter.sceneUploadVideo.doSceneResume(chatMsg_0.strClientMsgId);
			}
		}
		public static void rereceiveTalkMsg(ChatMsg chatMsg_0)
		{
			if (chatMsg_0 == null)
			{
				return;
			}
			if (chatMsg_0.nIsSender == 1)
			{
				return;
			}
			if (chatMsg_0.IsVoice())
			{
				DownloadVoiceService.reDownloadVoiceInfo(chatMsg_0);
			}
		}
		public static string GetVoiceUrl(ChatMsg chatMsg_0)
		{
			return ChatMsgMgr.getMsgVoice(chatMsg_0);
		}
		public static string rePackImageLenXml(int hdImgLen)
		{
			return string.Concat(new object[]
			{
				"<msg><img length=\"",
				hdImgLen,
				"\" hdlength=\"",
				hdImgLen,
				"\" /></msg>"
			});
		}
		public static bool getImageLenFromContent(ChatMsg mChatMsg, ref int imgLen, ref int hdImgLen)
		{
			if (mChatMsg == null)
			{
				return false;
			}
			if (string.IsNullOrEmpty(mChatMsg.strContent))
			{
				Log.smethod_5("ChatMsgMgr", "getImageMsg, input invalid para");
				return false;
			}
			string text = mChatMsg.strContent;
			if (ContactMgr.getUserType(mChatMsg.strTalker) == ContactMgr.UserType.UserTypeChatRoom)
			{
				int num = mChatMsg.strContent.IndexOf('\n');
				text = mChatMsg.strContent.Substring(num + 1);
			}
			bool result;
			try
			{
				XDocument xDocument = XDocument.Parse(text);
				XElement xElement = xDocument.Element("msg");
				XAttribute xAttribute = xElement.Element("img").Attribute("length");
				if (xAttribute != null)
				{
					imgLen = int.Parse(xAttribute.get_Value());
				}
				xAttribute = xElement.Element("img").Attribute("hdlength");
				if (xAttribute != null)
				{
					hdImgLen = int.Parse(xAttribute.get_Value());
				}
				return true;
			}
			catch (Exception ex)
			{
				Log.smethod_1("ChatMsgMgr", ex.get_Message());
				result = false;
			}
			return result;
		}
		public static bool getQQOfflineMsg(string xmlStr, ref string displayName, ref string msgContent)
		{
			if (string.IsNullOrEmpty(xmlStr))
			{
				Log.smethod_5("ChatMsgMgr", "getQQOfflineMsg, input invalid para");
				return false;
			}
			bool result;
			try
			{
				XDocument xDocument = XDocument.Parse(xmlStr);
				XElement xElement = xDocument.Element("msg");
				XElement xElement2 = xElement.Element("from").Element("displayname");
				if (xElement2 != null)
				{
					displayName = xElement2.get_Value();
				}
				xElement2 = xElement.Element("content").Element("t");
				if (xElement2 != null)
				{
					msgContent = xElement2.get_Value();
				}
				return true;
			}
			catch (Exception ex)
			{
				Log.smethod_1("ChatMsgMgr", ex.get_Message());
				result = false;
			}
			return result;
		}
		public static Contact getShareCardMsgContact(string xmlStrContent)
		{
			if (string.IsNullOrEmpty(xmlStrContent))
			{
				Log.smethod_5("ChatMsgMgr", "getShareCardMsgContact, input invalid para");
				return null;
			}
			Contact contact = new Contact();
			Contact result;
			try
			{
				XDocument xDocument = XDocument.Parse(xmlStrContent);
				XElement xElement = xDocument.Element("msg");
				XAttribute xAttribute = xElement.Attribute("username");
				if (xAttribute != null)
				{
					contact.strUsrName = xAttribute.get_Value();
				}
				xAttribute = xElement.Attribute("nickname");
				if (xAttribute != null)
				{
					contact.strNickName = xAttribute.get_Value();
				}
				xAttribute = xElement.Attribute("fullpy");
				if (xAttribute != null)
				{
					contact.strQuanPin = xAttribute.get_Value();
				}
				xAttribute = xElement.Attribute("shortpy");
				if (xAttribute != null)
				{
					contact.strPYInitial = xAttribute.get_Value();
				}
				xAttribute = xElement.Attribute("imagestatus");
				if (xAttribute != null)
				{
					contact.nImgFlag = uint.Parse(xAttribute.get_Value());
				}
				xAttribute = xElement.Attribute("province");
				if (xAttribute != null)
				{
					contact.strProvince = xAttribute.get_Value();
				}
				xAttribute = xElement.Attribute("city");
				if (xAttribute != null)
				{
					contact.strCity = xAttribute.get_Value();
				}
				xAttribute = xElement.Attribute("sign");
				if (xAttribute != null)
				{
					contact.strSignature = xAttribute.get_Value();
				}
				xAttribute = xElement.Attribute("percard");
				if (xAttribute != null)
				{
					contact.nPersonalCard = uint.Parse(xAttribute.get_Value());
				}
				xAttribute = xElement.Attribute("sex");
				if (xAttribute != null)
				{
					contact.nSex = int.Parse(xAttribute.get_Value());
				}
				xAttribute = xElement.Attribute("alias");
				if (xAttribute != null)
				{
					contact.strAlias = xAttribute.get_Value();
				}
				return contact;
			}
			catch (Exception ex)
			{
				Log.smethod_1("ChatMsgMgr", ex.get_Message());
				result = null;
			}
			return result;
		}
		public static bool getVerifyMsg(string xmlStr, ref Contact contact_0, ref string content)
		{
			if (!string.IsNullOrEmpty(xmlStr) && contact_0 != null)
			{
				bool result;
				try
				{
					XDocument xDocument = XDocument.Parse(xmlStr);
					XElement xElement = xDocument.Element("msg");
					XAttribute xAttribute = xElement.Attribute("fromusername");
					if (xAttribute != null)
					{
						contact_0.strUsrName = xAttribute.get_Value();
					}
					xAttribute = xElement.Attribute("fromnickname");
					if (xAttribute != null)
					{
						contact_0.strNickName = xAttribute.get_Value();
					}
					xAttribute = xElement.Attribute("fullpy");
					if (xAttribute != null)
					{
						contact_0.strQuanPin = xAttribute.get_Value();
					}
					xAttribute = xElement.Attribute("shortpy");
					if (xAttribute != null)
					{
						contact_0.strPYInitial = xAttribute.get_Value();
					}
					xAttribute = xElement.Attribute("imagestatus");
					if (xAttribute != null)
					{
						contact_0.nImgFlag = uint.Parse(xAttribute.get_Value());
					}
					xAttribute = xElement.Attribute("province");
					if (xAttribute != null)
					{
						contact_0.strProvince = xAttribute.get_Value();
					}
					xAttribute = xElement.Attribute("city");
					if (xAttribute != null)
					{
						contact_0.strCity = xAttribute.get_Value();
					}
					xAttribute = xElement.Attribute("sign");
					if (xAttribute != null)
					{
						contact_0.strSignature = xAttribute.get_Value();
					}
					xAttribute = xElement.Attribute("percard");
					if (xAttribute != null)
					{
						contact_0.nPersonalCard = uint.Parse(xAttribute.get_Value());
					}
					xAttribute = xElement.Attribute("sex");
					if (xAttribute != null)
					{
						contact_0.nSex = int.Parse(xAttribute.get_Value());
					}
					xAttribute = xElement.Attribute("scene");
					if (xAttribute != null)
					{
						contact_0.nAddContactScene = uint.Parse(xAttribute.get_Value());
					}
					xAttribute = xElement.Attribute("content");
					if (xAttribute != null)
					{
						content = xAttribute.get_Value();
					}
					return true;
				}
				catch (Exception ex)
				{
					Log.smethod_1("ChatMsgMgr", ex.get_Message());
					result = false;
				}
				return result;
			}
			Log.smethod_5("ChatMsgMgr", "getVerifyMsg, input invalid para");
			return false;
		}
	}
}
